package com.cloudschool.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cloudschool.base.exception.CloudSchoolException;
import com.cloudschool.base.model.RestResponse;
import com.cloudschool.content.mapper.TeachplanMapper;
import com.cloudschool.content.mapper.TeachplanMediaMapper;
import com.cloudschool.content.model.dto.BindTeachPlanMediaDto;
import com.cloudschool.content.model.dto.SaveTeachplanDto;
import com.cloudschool.content.model.dto.TeachplanDto;
import com.cloudschool.content.model.po.Teachplan;
import com.cloudschool.content.model.po.TeachplanMedia;
import com.cloudschool.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chenhong
 * @version 1.0
 * @description 课程计划service接口实现类
 * @date 2023/3/9 16:01
 */
@Slf4j
@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplanTree(long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Transactional
    @Override
    public void saveOrUpdateTeachPlan(SaveTeachplanDto teachplanDto) {
        //课程计划id
        Long id = teachplanDto.getId();
        //修改课程计划
        if (id != null) {
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto, teachplan);
            teachplanMapper.updateById(teachplan);
        } else {
            //取出同父同级别的课程计划数量
            int count = getTeachPlanCount(teachplanDto.getCourseId(),
                    teachplanDto.getParentid());
            Teachplan teachPlanNew = new Teachplan();
            //设置排序号
            teachPlanNew.setOrderby(count + 1);
            BeanUtils.copyProperties(teachplanDto, teachPlanNew);
            teachplanMapper.insert(teachPlanNew);
        }
    }

    @Transactional
    @Override
    public TeachplanMedia associationMedia(BindTeachPlanMediaDto bindTeachplanMediaDto) {
        //教学计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if (teachplan == null) {
            CloudSchoolException.cast("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if (grade != 2) {
            CloudSchoolException.cast("只允许第二级教学计划绑定媒资文件");
        }
        //课程id
        Long courseId = teachplan.getCourseId();

        //先删除原来该教学计划绑定的媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, teachplanId));

        //再添加教学计划与媒资的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
        return teachplanMedia;
    }

    @Override
    public RestResponse deleteAssociationMedia(Long teachPlanId, String mediaId) {
        //课程id
        LambdaQueryWrapper<TeachplanMedia> queryWrapper =
                new LambdaQueryWrapper<TeachplanMedia>()
                        .eq(TeachplanMedia::getMediaId, mediaId)
                        .eq(TeachplanMedia::getTeachplanId, teachPlanId);
        teachplanMediaMapper.delete(queryWrapper);
        return RestResponse.success("删除成功");

    }

    private int getTeachPlanCount(long courseId, long parentId) {
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId, courseId);
        queryWrapper.eq(Teachplan::getParentid, parentId);
        return teachplanMapper.selectCount(queryWrapper);
    }
}
